package systest;

import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.testng.*;
import org.testng.annotations.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class AdminSearchBasketOrder extends systest.pages.TestBase {
  private boolean acceptNextAlert = true;
  private StringBuffer verificationErrors = new StringBuffer();

  @Test
  public void testAdminSearchBasketOrder() throws Exception {    driver.get(baseUrl + "/admin/");
    // ERROR: Caught exception [ERROR: Unsupported command [selectFrame | control | ]]
    driver.findElement(By.cssSelector("#I31 > #Lhref > div.text")).click();
    // ERROR: Caught exception [ERROR: Unsupported command [selectFrame | relative=up | ]]
    // ERROR: Caught exception [ERROR: Unsupported command [selectFrame | content_frame | ]]
    for (int second = 0;; second++) {
    	if (second >= 60) fail("timeout");
    	try { if (isElementPresent(By.id("article"))) break; } catch (Exception e) {}
    	Thread.sleep(1000);
    }

    driver.findElement(By.id("article")).clear();
    driver.findElement(By.id("article")).sendKeys("kl9");
    driver.findElement(By.name("search")).click();
    driver.findElement(By.linkText("поиск")).click();
    driver.findElement(By.name("sidBasket")).click();
    driver.findElement(By.id("chPos[1]10")).click();
    driver.findElement(By.name("save_order")).click();
    driver.findElement(By.id("dcm_cst_id_viewState")).click();
    // ERROR: Caught exception [ERROR: Unsupported command [waitForPopUp |  | 30000]]
    // ERROR: Caught exception [ERROR: Unsupported command [selectPopUp | null | ]]
    for (int second = 0;; second++) {
    	if (second >= 60) fail("timeout");
    	try { if (isElementPresent(By.id("flt_userlogin"))) break; } catch (Exception e) {}
    	Thread.sleep(1000);
    }

    driver.findElement(By.id("flt_userlogin")).clear();
    driver.findElement(By.id("flt_userlogin")).sendKeys("testme");
    driver.findElement(By.cssSelector("input[name=\"filter\"]")).click();
    driver.findElement(By.xpath("//tr[2]/td[7]")).click();
    // ERROR: Caught exception [ERROR: Unsupported command [selectWindow | null | ]]
    // ERROR: Caught exception [ERROR: Unsupported command [selectFrame | name=content_frame | ]]
    try {
      assertTrue(isElementPresent(By.id("ord_pmk_id")));
    } catch (Error e) {
      verificationErrors.append(e.toString());
    }
    new Select(driver.findElement(By.id("ord_pmk_id"))).selectByVisibleText("наличный расчет");
    driver.findElement(By.name("save_order")).click();
    // Warning: verifyTextPresent may require manual changes
    try {
      assertTrue(driver.findElement(By.cssSelector("BODY")).getText().matches("^[\\s\\S]*Заказ успешно оформлен[\\s\\S]*$"));
    } catch (Error e) {
      verificationErrors.append(e.toString());
    }
    // Warning: verifyTextNotPresent may require manual changes
    try {
      assertFalse(driver.findElement(By.cssSelector("BODY")).getText().matches("^[\\s\\S]*Error[\\s\\S]*$"));
    } catch (Error e) {
      verificationErrors.append(e.toString());
    }
    driver.findElement(By.linkText("перейти к его просмотру")).click();
    try {
      assertEquals("", driver.getTitle());
    } catch (Error e) {
      verificationErrors.append(e.toString());
    }
  }

  private boolean isElementPresent(By by) {
    try {
      driver.findElement(by);
      return true;
    } catch (NoSuchElementException e) {
      return false;
    }
  }

  private String closeAlertAndGetItsText() {
    try {
      Alert alert = driver.switchTo().alert();
      String alertText = alert.getText();
      if (acceptNextAlert) {
        alert.accept();
      } else {
        alert.dismiss();
      }
      return alertText;
    } finally {
      acceptNextAlert = true;
    }
  }
}